﻿using In66.Net.EventBus.Cap;

namespace In66.Net.Share.Domain
{
    public abstract class AggregateRoot : DomainEntity
    {
        public Lazy<IEventPublisher> EventPublisher => new(() =>
        {
            var httpContext = InfraHelper.Accessor.GetCurrentHttpContext();
            if (httpContext is not null)
            {
                return httpContext.RequestServices.GetRequiredService<IEventPublisher>();
            }

            if (ServiceLocator.Provider is not null)
            {
                return ServiceLocator.Provider.GetRequiredService<IEventPublisher>();
            }

            throw new NotImplementedException(nameof(IEventPublisher));
        });
    }
}
